/***************************************************************************************************
 *
 * Project: HZFlashCards
 *
 * Class: PrintingManager
 *
 * File name: PrintingManager.java
 *
 *
 * SVN:
 *
 * $Rev$: $Date$: $LastChangedBy$:
 *
 * $HeadURL$:
 *
 **************************************************************************************************/

package com.eastmountain.chinese.printing;

import java.awt.Dimension;
import java.util.TreeMap;
import java.util.Vector;

import com.eastmountain.chinese.db.flashcards.records.ChineseCharacterProperties;
import com.eastmountain.chinese.db.flashcards.records.ChineseElementProperties;
import com.eastmountain.chinese.db.flashcards.records.ChineseWordProperties;
import com.eastmountain.chinese.gui.HTMLCharacterToWordXRFListPane;
import com.eastmountain.chinese.gui.HTMLReviewListPane;
import com.eastmountain.chinese.gui.HTMLWordCharacterBreakdownListPane;
import com.eastmountain.chinese.gui.TableReviewListPane;
import com.eastmountain.chinese.gui.controllers.HTMLMenuBar;
import com.eastmountain.chinese.observable.HZFlashcardsDataModel;

public class PrintingManager {

    private HZFlashcardsDataModel hzFlashcardsDataModel = null;

    private HTMLMenuBar htmlMenuBar = null;

    private HTMLReviewListPane htmlReviewListPane = null;
    private HTMLCharacterToWordXRFListPane htmlCharacterWordXRFListPane = null;
    private HTMLWordCharacterBreakdownListPane htmlWordCharacterBreakdownListPane  = null;
    
    private Vector<ChineseElementProperties> chineseElementReviewList = null;
    private Vector<ChineseWordProperties> chineseWordPropertiesGroup = null;
    private Vector<ChineseCharacterProperties> chineseCharacterPropertiesGroup = null;


    private Dimension[] fieldDimensions = null;
    private int[] pageBreaks; // array of page positions
    private String[][] fieldValues;

    private static final String SAMPLE_CHINESE_CHARACTER = "\u672c";
    private static final String SAMPLE_CHINESE_WORD = "\u65e5\u672c\u8a9e\u6587\u5b57\u5217";

    private static final String SAMPLE_CHINESE_CHARACTER_PINYIN = "xiang";
    private static final String SAMPLE_CHINESE_WORD_PINYIN = "xiangxiangxiangxiangxiang";

    private static final String SAMPLE_CHINESE_CHARACTER_MEANING = "some meaning";
    private static final String SAMPLE_CHINESE_WORD_MEANING = "some meaning";

    private static final String SAMPLE_LESSON_NUMBER = "999";
    private static final String SAMPLE_LESSON_LEVEL = "C";

    private static final String[] CHARACTER_LENGTH_FIELD_VALUES
        = { PrintingManager.SAMPLE_CHINESE_CHARACTER,
            PrintingManager.SAMPLE_CHINESE_CHARACTER_PINYIN,
            PrintingManager.SAMPLE_CHINESE_CHARACTER_MEANING,
            PrintingManager.SAMPLE_LESSON_NUMBER,
            PrintingManager.SAMPLE_LESSON_LEVEL
          };

    private static final String[] WORD_LENGTH_FIELD_VALUES
        = { PrintingManager.SAMPLE_CHINESE_WORD,
            PrintingManager.SAMPLE_CHINESE_WORD_PINYIN,
            PrintingManager.SAMPLE_CHINESE_WORD_MEANING,
            PrintingManager.SAMPLE_LESSON_NUMBER,
            PrintingManager.SAMPLE_LESSON_LEVEL
          };

    public PrintingManager() {

    }

    public void printReviewList( Vector<ChineseElementProperties> chineseElementReviewList ) {

        System.out.println( "\nPrintingManager.printReviewList() invoked\n" );
        
        this.chineseElementReviewList = chineseElementReviewList;

        htmlReviewListPane = new HTMLReviewListPane();
        htmlReviewListPane.displayHTML( chineseElementReviewList );

    }

    public void printReviewList( TreeMap<String, ChineseElementProperties> chineseElementReviewList ) {

        printReviewList( new Vector( chineseElementReviewList.values() ) );
                
    }

    public void printCharacterWordXRFList( String hanZi, Vector<ChineseWordProperties> chineseWordPropertiesGroup ) {

        System.out.println( "\nPrintingManager.printCharacterWordXRFList() invoked\n" );

        this.chineseWordPropertiesGroup = chineseWordPropertiesGroup;

        htmlCharacterWordXRFListPane = new HTMLCharacterToWordXRFListPane();
        htmlCharacterWordXRFListPane.displayHTML( hanZi, chineseWordPropertiesGroup );

    }
    
    public void printWordCharacterBreakdownListPane( String word, Vector<ChineseCharacterProperties> chineseCharacterPropertiesGroup ) {

        System.out.println( "\nPrintingManager.printWordCharacterBreakdownListPane() invoked\n" );

        this.chineseCharacterPropertiesGroup = chineseCharacterPropertiesGroup;

        htmlWordCharacterBreakdownListPane = new HTMLWordCharacterBreakdownListPane();
        htmlWordCharacterBreakdownListPane.displayHTML( word, chineseCharacterPropertiesGroup );

    }

/*
    public void doPrinting() {

        PrinterJob job = PrinterJob.getPrinterJob();

        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        PageFormat pf = job.pageDialog( aset );

        job.setPrintable( this, pf );
        boolean ok = job.printDialog( aset );

        if ( ok ) {

            try {

                job.print( aset );

            } catch ( PrinterException ex ) {

                 The job did not successfully complete

            }

        }

    }

    public int print( Graphics graphics, PageFormat pageFormat, int pageIndex ) throws PrinterException {

        int x = 0;
        int y = 0;

        Font font = new Font( "Serif", Font.BOLD, 24 );

        Graphics2D graphics2D = (Graphics2D) graphics;

        if ( fieldDimensions == null ) {

            fieldDimensions = calculateFieldDimensions( graphics2D, font );

        }

        if ( pageBreaks == null ) {

            pageBreaks = calculatePageBreaks( pageFormat );

        }

        if ( pageIndex > pageBreaks.length ) {

            return NO_SUCH_PAGE;

        }


         * User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid
         * clipping Since we are drawing text we


        System.out.println(  "pageFormat.getImageableX(): " + pageFormat.getImageableX()
                             + "pageFormat.getImageableY(): " + pageFormat.getImageableY() );

        graphics2D.translate( pageFormat.getImageableX(), pageFormat.getImageableY() );


         * Draw each line that is on this page. Increment 'y' position by
         * lineHeight for each line.


        int start = ( pageIndex == 0 ) ? 0 : pageBreaks[ pageIndex - 1 ];
        int end = ( pageIndex == pageBreaks.length ) ? fieldValues.length : pageBreaks[ pageIndex ];

        for ( int line = start; line < end; line++ ) {

            x = 0;

            y += fieldDimensions[ 0 ].height;

            for ( int i = 0; i < ChineseElementProperties.NUMBER_OF_FIELD_VALUES; i++ ) {

                graphics2D.drawString( fieldValues[ line ][ i ], x, y );
                x = x + fieldDimensions[ i ].width + 5;

            }

        }

         tell the caller that this page is part of the printed document

        return PAGE_EXISTS;

    }

    *//*************************************************
     *
     *   calculatePageBreaks
     *
     ************************************************//*

    private int[] calculatePageBreaks( PageFormat pageFormat ) {

        int linesPerPage;
        int numBreaks;
        int[] pageBreaks;

        linesPerPage = (int) ( pageFormat.getImageableHeight() / fieldDimensions[ 0 ].height);

        numBreaks = ( fieldValues.length - 1) / linesPerPage;
        pageBreaks = new int[numBreaks];

        for ( int b = 0; b < numBreaks; b++ ) {

            pageBreaks[ b ] = (b + 1) * linesPerPage;

        }


        return( pageBreaks );

    }

    private Dimension[] calculateFieldDimensions( Graphics2D graphics2D, Font font ) {

        Dimension[] dimensions = new Dimension[ ChineseElementProperties.NUMBER_OF_FIELD_VALUES ];
        int advance;

        FontMetrics metrics = graphics2D.getFontMetrics( font );

        int lineHeight = metrics.getHeight();

        // calculate the size of each field on the print image

        for ( int i = 0; i < ChineseElementProperties.NUMBER_OF_FIELD_VALUES; i++ ) {

            advance = metrics.stringWidth( CHARACTER_LENGTH_FIELD_VALUES[ i ] );

            dimensions[ i ] = new Dimension( advance + 2, lineHeight + 2 );

        }

        return (dimensions);

    }

    private String[][] extractFieldValues( Vector<ChineseElementProperties> chineLessonFlashCardSet ) {

        String[][] fieldValues
            = new String[chineLessonFlashCardSet.size()][ ChineseElementProperties.NUMBER_OF_FIELD_VALUES ];
        String lessonNumber= "";


        for ( int i = 0; i < chineLessonFlashCardSet.size(); i++ ) {

            fieldValues[ i ][ 0 ] = chineLessonFlashCardSet.elementAt( i ).getChineseElement();
            fieldValues[ i ][ 1 ] = chineLessonFlashCardSet.elementAt( i ).getPinyin();
            fieldValues[ i ][ 2 ] = chineLessonFlashCardSet.elementAt( i ).getEnglish();
            lessonNumber = Integer.toString( chineLessonFlashCardSet.elementAt( i ).getLessonNumber() );
            fieldValues[ i ][ 3 ] = lessonNumber;
            fieldValues[ i ][ 4 ] = chineLessonFlashCardSet.elementAt( i ).getLevel();
        }

        return( fieldValues );

    }

    public void createHTMLListPanel( Vector<ChineseElementProperties> chineLessonFlashCardSet ) {

        JFrame frame = new JFrame( "Character Review List" );

        frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );

        htmlListPane = new HTMLListPane( chineLessonFlashCardSet );
        htmlListPane.setMinimumSize( new Dimension( 200, 200 ) );

        try {

            htmlMenuBar = new HTMLMenuBar( htmlListPane );

        } catch ( DBAccessException exception ) {

            exception.printStackTrace();

        }

        frame.setJMenuBar( htmlMenuBar );

        frame.getContentPane().add( htmlListPane, BorderLayout.CENTER );

        frame.pack();
        frame.setVisible( true );

    }
*/
}
